﻿// --------------------------------------------------------------------------------------------------------------------
// <copyright file="ColumnarCodeGenerator.cs" company="Data Ductus Malmö">
//   (c) 2010 - 2012 Data Ductus Malmö AB
// </copyright>
// --------------------------------------------------------------------------------------------------------------------

namespace DataDuctus.Snmp.Persistor.Core.CodeGen
{
  using System;
  using System.IO;

  using DataDuctus.Snmp.Persistor.Core.Schema;

  /// <summary>
  /// The columnar code generator.
  /// </summary>
  public class ColumnarCodeGenerator : IColumnarCodeGenerator
  {
    #region Public Properties

    /// <summary>
    /// Gets the type.
    /// </summary>
    public CodeGeneratorTypes Type
    {
      get
      {
        return CodeGeneratorTypes.Columnar;
      }
    }

    #endregion

    #region Public Methods and Operators

    /// <summary>
    /// The generate.
    /// </summary>
    /// <param name="item">
    /// The item.
    /// </param>
    /// <param name="entry">
    /// The entry.
    /// </param>
    /// <param name="table">
    /// The table.
    /// </param>
    /// <param name="profile">
    /// The profile.
    /// </param>
    /// <param name="codeStream">
    /// The code stream.
    /// </param>
    /// <param name="config">
    /// The config.
    /// </param>
    public void Generate(
      ISchemaItem item, 
      ISchemaItem entry, 
      ISchemaItem table, 
      ICodeGeneratorProfile profile, 
      TextWriter codeStream, 
      CodeGeneratorConfiuguration config)
    {
      string propertyType = profile.GeneratePropertyTypeName(item, this);
      string propertyName = profile.GeneratePropertyName(item, this);
      bool isAutoProperty = profile.IsAutoProperty(item, this);
      bool isIndexColumn = profile.IsIndexColumn(item, entry, table, this);

      if (isIndexColumn)
      {
        bool isForignKey = profile.IsForeignKey(item, entry, table, this);

        codeStream.WriteLine(!isForignKey ? "\t\t[Index]" : "\t\t[Index(ForeignKey = \"{0}\"");
      }

      codeStream.WriteLine("\t\tpublic {0} {1}", propertyType, propertyName);
      codeStream.WriteLine("\t\t{");
      codeStream.Write("\t\tget");

      if (!isAutoProperty)
      {
        codeStream.WriteLine("{0}\t\t{{", Environment.NewLine);
        codeStream.WriteLine("\t\treturn {0};", profile.GenerateFieldName(item));
        codeStream.WriteLine("\t\t}");
      }
      else
      {
        codeStream.WriteLine(";");
      }

      codeStream.WriteLine(string.Empty);
      codeStream.Write("\t\tset");

      if (!isAutoProperty)
      {
        codeStream.WriteLine("{0}\t\t{{", Environment.NewLine);
        codeStream.WriteLine("\t\t{0} = value;", profile.GenerateFieldName(item));
        codeStream.WriteLine("\t\t}");
      }
      else
      {
        codeStream.WriteLine(";");
      }

      codeStream.WriteLine("}");
    }

    /// <summary>
    /// The generate.
    /// </summary>
    /// <param name="item">
    /// The item.
    /// </param>
    /// <param name="profile">
    /// The profile.
    /// </param>
    /// <param name="codeStream">
    /// The code stream.
    /// </param>
    /// <param name="config">
    /// The config.
    /// </param>
    /// <exception cref="NotImplementedException">Always thrown.</exception>
    public void Generate(
      ISchemaItem item, ICodeGeneratorProfile profile, TextWriter codeStream, CodeGeneratorConfiuguration config)
    {
      throw new NotImplementedException();
    }

    #endregion
  }
}